<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\PrizeType;
use app\common\model\Prize as PrizeModel;
use app\common\model\Product;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;

class Prize extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Prize';

    public $append = ['type_text'];

    public function index()
    {
        return $this->setTable();
    }

    //自动构建table,按需修改
    public function setTable()
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'type_text', 'title' => '类型'])
            ->addRow(['field' => 'image', 'title' => '图标', 'width' => 90, 'templet' => '<span><img src="{{d.image}}" width="40"></span>'])
            ->addRow(['field' => 'amount', 'title' => '金额'])
            ->addRow(['field' => 'odds', 'title' => '中奖几率', 'width' => 200, 'templet' => '<div>{{d.odds*100}}%</div>'])
            ->addRow(['field' => 'count', 'title' => '需抽奖次数', 'width' => 200])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 80]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addTopButtons(['log' => ['title' => '修改日志', 'type'  => 'log']])
            ->addRightButtons(['edit' => ['title' => '编辑', 'width' => 1920, 'height' => 1080]])
            ->setSearch($this->setSearch())
            ->fetch('index');
    }

    public function setSearch()
    {
        return TableFront::getInstance();

    }

    public function edit($name = '', $id = '', $tpl = 'prize/edit', $info = '')
    {
        View::assign([
            'id' => $id,
            'info' => PrizeModel::find($id),
            'type' => PrizeType::where('is_del', 0)->field('id, title')->select(),
            'product' => Product::where(['is_del'=>0, 'status'=>1, 'business_id'=>8])->select()
        ]);
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
